#include <opengl_demo/qls_opengl_demo.h>
#include "ui_qls_opengl_demo.h"

#include <QTimer>

#include <cmath>

#include <qrender/box_renderer.h>
#include <opengl_demo/render_form.h>

#include "math.h"
#include "dice_renderer.h"

static QString getGlString(QOpenGLFunctions *functions, GLenum name)
{
    if (const GLubyte *p = functions->glGetString(name))
        return QString::fromLatin1(reinterpret_cast<const char *>(p));
    return QString();
}

OpenGLDemo::OpenGLDemo(QWidget *parent)
    : QWidget(parent), ui(new Ui::OpenGLDemo)
{
    ui->setupUi(this);

    render_ = new RenderForm(this);
    render_->setRenderEmbeded(true, ui->gly_render);

    // demo 默认 DH 参数
    std::unordered_map<std::string, std::vector<double>> dh;
    dh["a"] = std::vector<double>{ 0, 0, 0.408, 0.376, 0, 0 };
    dh["alpha"] =
        std::vector<double>{ 0, -M_PI / 2., M_PI, M_PI, -M_PI / 2., M_PI / 2. };
    dh["d"] = std::vector<double>{ 0.122, 0.1215, 0, 0, 0.1025, 0.094 };
    dh["theta"] = std::vector<double>{ M_PI, -M_PI / 2., 0, -M_PI / 2., 0, 0 };

    render_->reload("demo", dh);

    render_->setRealJointPositions({ 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 });
    render_->setTargetJointPositions({ 0.0, -100, 10, 25, 90, 0.0 });

    // 路点测试
    //    for (qint64 i = 0; i < 100; i++) {
    //        render_->addWaypoint((void *)i,
    //                             { 0.01 * i, 0.02 * i, 0.01 * i, 0.0, 0.0, 0.0
    //                             });
    //    }

    // 点云测试
    //    std::vector<std::vector<double>> points;
    //    for (int x = 0; x < 100; x++) {
    //        for (int y = 0; y < 100; y++) {
    //            for (int z = 0; z < 100; z++) {
    //                points.push_back({ 0.01 * x, 0.01 * y, 0.01 * z });
    //            }
    //        }
    //    }
    //    render_->addPointCloud(points);

    // 纹理测试
    //    render_->addRenderer(std::make_shared<DiceRenderer>());

    ui->tab->installEventFilter(this);
    ui->tab_2->installEventFilter(this);

    connect(ui->pushButton_2, &QPushButton::clicked, this,
            &OpenGLDemo::onPushButton2Clicked);
    connect(ui->pushButton_3, &QPushButton::clicked, this,
            &OpenGLDemo::onPushButton2Clicked);
}

OpenGLDemo::~OpenGLDemo()
{
    delete ui;
}

bool OpenGLDemo::eventFilter(QObject *watched, QEvent *event)
{
    // 切换模型渲染界面嵌入位置
    if (watched == ui->tab) {
        if (event->type() == QEvent::Show) {
            render_->setRenderEmbeded(true, ui->gly_render);
        }

        if (event->type() == QEvent::Hide) {
            render_->setRenderEmbeded(false, ui->gly_render);
        }
    }

    if (watched == ui->tab_2) {
        if (event->type() == QEvent::Show) {
            render_->setRenderEmbeded(true, ui->gly_render_3);
        }

        if (event->type() == QEvent::Hide) {
            render_->setRenderEmbeded(false, ui->gly_render_3);
        }
    }

    return QWidget::eventFilter(watched, event);
}

void OpenGLDemo::onPushButton2Clicked()
{
    static quint64 i = 0;
    render_->addWaypoint((void *)i,
                         { 0.01 * i, 0.02 * i, 0.01 * i, 0.0, 0.0, 0.0 });
    i++;
}
